package com.japp.sys.web;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import com.japp.base.BaseAction;
import com.japp.sys.bean.UserBean;
import com.japp.sys.service.UserService;
@Scope("prototype")
@Controller
public class LoginAction extends BaseAction<UserBean> {
	
	private static final long serialVersionUID = -155730993938501274L;
	@Autowired
	private UserService service;
	private UserBean user = new UserBean();	
	
	public String login(){
		if(user.getU_login_id()==null||user.getU_login_pwd()==null){
			return ERROR;
		}
		if(session.getAttribute("u_id")!=null&&session.getAttribute("u_name")!=null){
			return SUCCESS;
		}
		user = service.findUserByIdAndPwd(user);
		if(user!=null&&user.getU_id()!=0){
			session.setAttribute("user", user);	
			session.setAttribute(USERID, user.getU_id());				//用户ID
			session.setAttribute(USERLOGINID, user.getU_login_id());	//登录ID
			session.setAttribute(USERLTD, user.getU_ltd());				//部门ID
			session.setAttribute(USERLTDCODE, user.getU_ltd_code());	//部门编码
			session.setAttribute(USERLTDNAME, user.getU_ltd_name());	//部门名称
			session.setAttribute(USERNAME, user.getU_name());			//用户名称
			session.setAttribute(USERCODE, user.getU_code());			//用户编码
			session.setAttribute(USERPHONE, user.getU_phone());			//用户联系方式
			session.setAttribute(USERROLE, user.getU_role());			//角色ID
			session.setAttribute(USERPOWER, user.getU_power());			//用户级别
			result = "登陆成功!";
			return SUCCESS;
		}else{
			if(session!=null)session.invalidate();
			result = "登陆失败,用户名或密码错误!";
			return ERROR;
		}
	}
	/**
	 * 注销登陆
	 * @return
	 */
	public String loginOut(){
		if(session!=null){
			session.removeAttribute(USERID);
			session.removeAttribute(USERLOGINID);
			session.removeAttribute(USERNAME);
			session.removeAttribute(USERPHONE);
			session.removeAttribute(USERLTD);
			session.removeAttribute(USERLTDCODE);
			session.removeAttribute(USERLTDNAME);
			session.removeAttribute(USERROLE);
			session.removeAttribute(USERPOWER);
			session.invalidate();
		}
		return SUCCESS;
	}
	
	/**
	 * 跳转请勿删除
	 * @return
	 */
	public String forword(){
		setResult(result);
		return SUCCESS;
	}
	
	/*=========GET SET 开始==========*/

	public UserBean getUser() {
		return user;
	}
	public void setUser(UserBean user) {
		this.user = user;
	}
	
	/*=========GET SET 结束==========*/
}
